﻿using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Test.Console.SQLite;
using xu.TinyDBConfigProvider;

Console.WriteLine("Hello, World!");

ServiceCollection serviceCollection = new ServiceCollection();

IConfigurationBuilder builder = new ConfigurationBuilder();
builder.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
IConfigurationRoot? configuration = builder.Build();
string connStr = configuration.GetConnectionString("DefaultConnection");
builder.AddTinyDBConfiguration(() => new SqliteConnection(connStr), reloadOnChange: true, reloadInterval: TimeSpan.FromSeconds(10));
configuration = builder.Build();

serviceCollection.AddOptions().Configure<LogConfig>(e => configuration.GetSection("log").Bind(e));

serviceCollection.AddScoped<TestLogConfig>();

using (var sp = serviceCollection.BuildServiceProvider())
{
    var testLogConfig = sp.GetRequiredService<TestLogConfig>();
    testLogConfig.Test();
}

Console.WriteLine("connStr = " + configuration.GetConnectionString("DefaultConnection"));
Console.WriteLine("connStr = " + configuration.GetSection("log:minLevel").Value);
Console.WriteLine("log:logFilePath = " + configuration.GetSection("log:logFilePath").Value);
var log = configuration.GetSection("log").Get<LogConfig>();
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(log));

var aa = configuration.GetValue<string>("log");
//var a2 = tinyDBConfig.GetSection("Api:Jwt");
var log2 = configuration.GetSection("log").Get<LogConfig>();
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(log));
Console.WriteLine("LogFilePath = " + log.LogFilePath);
Console.WriteLine("log:logFilePath = " + configuration.GetSection("log:logFilePath").Value);
Console.WriteLine("log:logFilePath = " + configuration.GetValue<string>("log:logFilePath"));
Console.WriteLine("log:ApiUri = " + configuration.GetValue<string>("log:apiUri"));
Console.WriteLine("connStr = " + configuration.GetConnectionString("DefaultConnection"));
Console.WriteLine("connStr = " + configuration.GetConnectionString("DefaultConnection"));
Console.WriteLine("log:minLevel = " + configuration.GetValue<string>("log:minLevel"));

Task.Run(() =>
{
    while (true)
    {
        Console.WriteLine("时间：" + DateTime.Now);
        var log = configuration.GetSection("log").Get<LogConfig>();
        Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(log));
        Console.WriteLine("LogFilePath：" + log.LogFilePath);
        Console.WriteLine("log:minLevel = " + configuration.GetValue<string>("log:minLevel"));

        Thread.Sleep(5000);
    }
});

Console.ReadKey();